package com.mdd.admin.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdd.admin.service.order.IOrderLeaseDetailsService;
import com.mdd.admin.service.order.ISwitchedCarRecordService;
import com.mdd.admin.validate.order.SwitchedCarRecordCreateValidate;
import com.mdd.admin.vo.order.OrderLeaseDetailVo;
import com.mdd.common.entity.order.OrderInfo;
import com.mdd.common.entity.order.OrderLeaseDetails;
import com.mdd.common.enums.OperationTypeEnum;
import com.mdd.common.enums.OrderEnum;
import com.mdd.common.mapper.order.OrderLeaseDetailsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * 租赁订单详情实现类
 * @author LikeAdmin
 */
@Service
public class OrderLeaseDetailsServiceImpl extends ServiceImpl<OrderLeaseDetailsMapper,OrderLeaseDetails> implements IOrderLeaseDetailsService {

    @Resource
    OrderLeaseDetailsMapper orderLeaseDetailsMapper;
    @Resource
    ISwitchedCarRecordService iSwitchedCarRecordService;

    @Override
    public void buildOrChangeVehicle(Long orderId, Long vehicleId, OrderInfo orderInfo) {
        LambdaQueryWrapper<OrderLeaseDetails> detailsLambdaQueryWrapper=new LambdaQueryWrapper<>();
        detailsLambdaQueryWrapper.eq(OrderLeaseDetails::getOrderId,orderId);
        OrderLeaseDetails orderLeaseDetails = orderLeaseDetailsMapper.selectOne(detailsLambdaQueryWrapper);
        SwitchedCarRecordCreateValidate switchedCarRecord=new SwitchedCarRecordCreateValidate();
        if (Objects.nonNull(orderLeaseDetails.getVehicleId())){
            //todo 绑定车辆变更订单状态，启动扣款任务
            orderInfo.setStatus(OrderEnum.UNDER_WAY.getCode());
            switchedCarRecord.setOperationType(OperationTypeEnum.BUILD_VEHICLE.getCode());
        }else {
            switchedCarRecord.setOperationType(OperationTypeEnum.CHANGE_VEHICLE.getCode());
        }
        orderLeaseDetails.setVehicleId(vehicleId);
        orderLeaseDetailsMapper.updateById(orderLeaseDetails);

        iSwitchedCarRecordService.add(switchedCarRecord);
    }

    @Override
    public OrderLeaseDetailVo getOrderLeaseByOrderId(Long orderId) {
        LambdaQueryWrapper<OrderLeaseDetails> orderLeaseDetailsWrapper=new LambdaQueryWrapper<>();
        orderLeaseDetailsWrapper.eq(OrderLeaseDetails::getOrderId,orderId);
        OrderLeaseDetails orderLeaseDetails = orderLeaseDetailsMapper.selectOne(orderLeaseDetailsWrapper);
        OrderLeaseDetailVo orderLeaseDetailVo=new OrderLeaseDetailVo();
        BeanUtils.copyProperties(orderLeaseDetails,orderLeaseDetailVo);
        return orderLeaseDetailVo;
    }
    @Transactional
    @Override
    public void updateLeaseDetail(Long orderId, Long carId) {
        LambdaQueryWrapper<OrderLeaseDetails> orderLeaseDetailsWrapper=new LambdaQueryWrapper<>();
        orderLeaseDetailsWrapper.eq(OrderLeaseDetails::getOrderId,orderId);
        OrderLeaseDetails orderLeaseDetails = orderLeaseDetailsMapper.selectOne(orderLeaseDetailsWrapper);
        orderLeaseDetails.setVehicleId(carId);
        orderLeaseDetailsMapper.updateById(orderLeaseDetails);
    }
    @Transactional
    @Override
    public void updateLeaseDetailStatus(Long orderId, Integer status) {
        LambdaQueryWrapper<OrderLeaseDetails> orderLeaseDetailsWrapper=new LambdaQueryWrapper<>();
        orderLeaseDetailsWrapper.eq(OrderLeaseDetails::getOrderId,orderId);
        OrderLeaseDetails orderLeaseDetails = orderLeaseDetailsMapper.selectOne(orderLeaseDetailsWrapper);
        orderLeaseDetails.setOrderVehicleStatus(status);
        orderLeaseDetails.setUpdateTime(new Date());
        orderLeaseDetailsMapper.updateById(orderLeaseDetails);
    }
}
